
#import "PUUpdateView.h"

@implementation PUUpdateView
-(instancetype)initWithCPUpdateViewInfo:(NSString *)info version:(NSString *)version is_force:(int)is_force link:(NSString *)link link_type:(int)link_type
{
    self = [super init];
    if (self) {
       
        self.desc=info;
        self.version=version;
        self.is_force=is_force;
        self.download_url=link;
        self.link_type=link_type;
        [self _setupUI];
    }
    return self;
}
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
}

- (void)_setupUI
{
    
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:0.5/1.0];

    //backgroundView
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor=[UIColor clearColor];
    bgView.bounds = CGRectMake(0, 0, self.frame.size.width*0.850, self.frame.size.width*0.850*1.15);
    bgView.center = self.center;
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = self.frame.size.width*0.042;
    [self addSubview:bgView];
    
    UIView *whiteView = [[UIView alloc]init];
    whiteView.backgroundColor=[UIColor whiteColor];
    [bgView addSubview:whiteView];
    
   //背景图
    UIImageView *redPacketImage = [[UIImageView alloc]init];
    redPacketImage.image = [UIImage imageNamed:@"版本升级BG"];
    redPacketImage.frame=CGRectMake(0, 0, bgView.frame.size.width,bgView.frame.size.width*0.536);
    [bgView addSubview:redPacketImage];
    
//    UILabel *newVLab = [[UILabel alloc] init];
//    newVLab.frame=CGRectMake(bgView.frame.size.width*0.108, bgView.frame.size.width*0.064, bgView.frame.size.width-bgView.frame.size.width*0.108, bgView.frame.size.width*0.118);
//    newVLab.text=@"发现新版本";
//    newVLab.font=[UIFont boldSystemFontOfSize:24];
//    newVLab.textAlignment=NSTextAlignmentLeft;
//    newVLab.textColor=[UIColor whiteColor];
//    [bgView addSubview:newVLab];
    
//    NSString *v=self.version;
//    UILabel *VLab = [[UILabel alloc] init];
//    VLab.frame=CGRectMake(bgView.frame.size.width*0.108, bgView.frame.size.width*0.183, bgView.frame.size.width-bgView.frame.size.width*0.108, bgView.frame.size.width*0.061);
//    VLab.text=[NSString stringWithFormat:@"%@",v];
//    VLab.font=[UIFont systemFontOfSize:16];
//    VLab.textAlignment=NSTextAlignmentLeft;
//    VLab.textColor=[UIColor whiteColor];
//    [bgView addSubview:VLab];
    
    UILabel *updateLab = [[UILabel alloc] init];
    updateLab.frame=CGRectMake(0, redPacketImage.frame.origin.y+redPacketImage.frame.size.height, bgView.frame.size.width, 17*KWidthScale);
    updateLab.text=[NSString stringWithFormat:@"V%@升级通知",self.version];
    updateLab.font=[UIFont systemFontOfSize:17];
    updateLab.textAlignment=NSTextAlignmentCenter;
    updateLab.textColor=[UIColor blackColor];
    [bgView addSubview:updateLab];
    
    
    
    UILabel *updateContenLab = [[UILabel alloc] init];
    updateContenLab.frame=CGRectMake(12*KWidthScale, updateLab.frame.origin.y+updateLab.frame.size.height+24*KWidthScale, bgView.frame.size.width-12*KWidthScale*2, bgView.frame.size.width*0.118);
    updateContenLab.text=self.desc;
    updateContenLab.font=[UIFont systemFontOfSize:15];
    updateContenLab.numberOfLines=0;
    updateContenLab.textAlignment=NSTextAlignmentLeft;
    updateContenLab.textColor=[UIColor colorWithHex:0x888888];
    [bgView addSubview:updateContenLab];
//    updateContenLab.backgroundColor=[UIColor redColor];
    
//    CGSize labs=CGSizeMake(bgView.frame.size.width-12*KWidthScale*2, 2000);
//    CGSize size=[updateContenLab.text boundingRectWithSize:labs options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
//
//    updateContenLab.frame=CGRectMake(12*KWidthScale, updateLab.frame.origin.y+updateLab.frame.size.height+24*KWidthScale, bgView.frame.size.width-12*KWidthScale*2, size.height);
    
    //设置行距，并根据文本计算高度
    ////// ////// ////// ////// ////// //////
    //富文本属性
    NSMutableDictionary *textDict = [NSMutableDictionary dictionary];
    //基本属性设置
    //字体颜色
    textDict[NSForegroundColorAttributeName] = [UIColor colorWithHex:0x888888];
    //字号大小
    textDict[NSFontAttributeName] = [UIFont systemFontOfSize:15.0];
    //段落样式
    NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
    //行间距
    paraStyle.lineSpacing = 5.0;
    //首行文本缩进
//  paraStyle.firstLineHeadIndent = 20.0;
    //使用
    //文本段落样式
    textDict[NSParagraphStyleAttributeName] = paraStyle;
    //赋值
    updateContenLab.attributedText = [[NSAttributedString alloc] initWithString:self.desc attributes:textDict];

    CGFloat size2 = [self getSpaceLabelHeight:self.desc withAttrDict:textDict withWidth:updateContenLab.frame.size.width];
    
    
    updateContenLab.frame=CGRectMake(12*KWidthScale, updateLab.frame.origin.y+updateLab.frame.size.height+24*KWidthScale, bgView.frame.size.width-12*KWidthScale*2, size2);
    
    
    //稍后再说
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [leftBtn setBackgroundColor:[UIColor whiteColor]];
    leftBtn.frame = CGRectMake(bgView.frame.size.width*0.037, updateContenLab.frame.origin.y+updateContenLab.frame.size.height+24*KWidthScale, bgView.frame.size.width*0.442, bgView.frame.size.width*0.442*0.34);
    leftBtn.clipsToBounds = YES;
    leftBtn.layer.cornerRadius =leftBtn.frame.size.height/2;
    leftBtn.layer.borderColor = [UIColor colorWithHex:0xFF729A].CGColor;
    leftBtn.layer.borderWidth = 1.0;
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitle:@"稍后再说" forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [leftBtn setTitleColor:[UIColor colorWithHex:0xFF729A] forState:UIControlStateNormal];
    [bgView addSubview:leftBtn];
    
    //立即升级
    UIButton *updateBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [updateBtn setBackgroundColor:[UIColor colorWithHex:0xFF729A]];
    updateBtn.frame = CGRectMake(bgView.frame.size.width*0.52, updateContenLab.frame.origin.y+updateContenLab.frame.size.height+24*KWidthScale, bgView.frame.size.width*0.442, bgView.frame.size.width*0.442*0.34);
    updateBtn.clipsToBounds = YES;
    updateBtn.layer.cornerRadius =updateBtn.frame.size.height/2;
       
    [updateBtn addTarget:self action:@selector(updateBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [updateBtn setTitle:@"立即升级" forState:UIControlStateNormal];
    updateBtn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    [updateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:updateBtn];
    
    if (self.is_force==1) {
        leftBtn.hidden=YES;
        updateBtn.frame = CGRectMake(bgView.frame.size.width*0.037, updateContenLab.frame.origin.y+updateContenLab.frame.size.height+24*KWidthScale, bgView.frame.size.width-bgView.frame.size.width*0.037*2, bgView.frame.size.width*0.442*0.34);
    }
    
   
    bgView.bounds = CGRectMake(0, 0, self.frame.size.width*0.850, updateBtn.frame.origin.y+updateBtn.frame.size.height+12*KWidthScale);
    whiteView.frame= CGRectMake(0, redPacketImage.frame.origin.y+redPacketImage.frame.size.height, bgView.frame.size.width, bgView.frame.size.height-(redPacketImage.frame.origin.y+redPacketImage.frame.size.height));
    
    
    
    //关闭按钮
//    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    cancelButton.frame = CGRectMake(self.frame.size.width/2-15,bgView.frame.origin.y+bgView.frame.size.height+30,30,30);
//    [cancelButton setImage:[UIImage imageNamed:@"closeView"] forState:UIControlStateNormal];
//    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
//    [self addSubview:cancelButton];
//
//    if (self.is_force==1) {
//        cancelButton.hidden=YES;
//    }
    
    [self showWithAlert:bgView];//添加动画的View
    
}
//计算有行距的文本的文本高度
-(CGFloat)getSpaceLabelHeight:(NSString *)str withAttrDict:(NSMutableDictionary *)dict withWidth:(CGFloat)width {
    CGSize size = [str boundingRectWithSize:CGSizeMake(width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size;
    return size.height;
}

-(void)leftBtnClick{
    [self dismissAlert];
    if ([self.delegate respondsToSelector:@selector(CPUpdateViewUpdateClose)]) {
        [self.delegate CPUpdateViewUpdateClose];
    }
}

-(void)updateBtnClick{
//    [self dismissAlert];
    if ([self.delegate respondsToSelector:@selector(CPUpdateViewUpdateBtnClicklink:link_type:is_force:)]) {
        [self.delegate CPUpdateViewUpdateBtnClicklink:self.download_url link_type:self.link_type is_force:self.is_force];
    }
}



/**
 添加Alert入场动画
 @param alert 添加动画的View
 */
- (void)showWithAlert:(UIView*)alert{
    
    CAKeyframeAnimation* animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = 0.2f;
    
    NSMutableArray *values = [NSMutableArray array];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.2, 1.2, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9, 0.9, 1.0)]];
//    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    
    animation.values = values;
    [alert.layer addAnimation:animation forKey:nil];
}

/** 添加Alert出场动画 */
- (void)dismissAlert{
    
//    [UIView animateWithDuration:0.3 animations:^{
//        self.transform = (CGAffineTransformMakeScale(1.6, 1.6));
//        self.backgroundColor = [UIColor clearColor];
//        self.alpha = 0;
//    }completion:^(BOOL finished) {
    [self removeFromSuperview];
        
//    } ];

}

//关闭
- (void)cancelAction
{
    [self dismissAlert];
    if ([self.delegate respondsToSelector:@selector(CPUpdateViewUpdateClose)]) {
        [self.delegate CPUpdateViewUpdateClose];
    }
   
}


@end
